package user

import (
	"github.com/gin-gonic/gin"
	"github.com/gomodule/redigo/redis"
	"smartLearn/models"
	"smartLearn/modules/cache"
	"smartLearn/modules/response"
)

func GetMenu(c *gin.Context)  {
	token:=c.GetHeader("authorization")
	rc:=cache.RedisClient.Get()
	defer rc.Close()

	uid,err:= redis.Int(rc.Do("get",token))

	if err!=nil {
		response.ShowError(c, "获取用户信息错误")
		return
	}


	menuModel:=models.SystemMenu{}
	menuList,err:=menuModel.GetByUserId(uid)
	if err!=nil {
		response.ShowError(c, err.Error())
	}
	var data = make(map[string]interface{}, 0)

	systemUserRole:=models.SystemUserRole{}
	systemUserRole.UserId=uid
	roleId,err:= systemUserRole.GetRoleIdByUid()
	if err!=nil {
		response.ShowError(c, err.Error())
		return
	}

	systemUser:=models.SystemUser{}
	systemUser.Id=uid
	systemUser,err=systemUser.GetById()
	if err!=nil {
		response.ShowError(c, err.Error())
		return
	}
	data["userid"]=uid
	data["username"]=systemUser.Username
	data["role"]=roleId
	data["menus"]=menuList

	response.ShowData(c,data)
	return
}
